package com.lib.image.preview

import android.os.Bundle
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import androidx.navigation.Navigation
import com.lib.image.ImageLoader
import com.lib.image.R
import com.lib.image.databinding.FragmentPhotoClipBinding
import com.lib.image.ex.showToast
import com.lib.image.until.FileUtil
import java.lang.Exception

/**
 * 图片裁剪
 * @author shalei
 */
class PhotoClipFragment: Fragment() {

    private val mImagePath by lazy { PhotoClipFragmentArgs.fromBundle(requireArguments()).imagePath }

    private lateinit var mViewModel: PhotoEditorViewModel

    private lateinit var mViewBinding: FragmentPhotoClipBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mViewModel = ViewModelProvider(requireActivity()).get(PhotoEditorViewModel::class.java)
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        mViewBinding = FragmentPhotoClipBinding.inflate(inflater, container, false)
        return mViewBinding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        mViewBinding.run {
            btnBack.setOnClickListener { Navigation.findNavController(view).popBackStack() }
            btnSave.setOnClickListener {
                if(null == rectLayout.bitmap) {
                    showToast("数据加载中...")
                    return@setOnClickListener
                }
                try {
                    val bitmap = rectLayout.clip()
                    val file = FileUtil.createImageFile(requireContext().applicationContext, "clip_${System.currentTimeMillis()}.jpg")
                    FileUtil.saveBitmap(requireContext(), bitmap, file, 100)
                    bitmap.recycle()
                    jump2EditPage(file.absolutePath)
                } catch (e: Exception){
                    showToast(e.message?:"数据异常")
                }
            }
        }

    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        loadUrlWithWG(mViewBinding.rectLayout.imageView, mImagePath)
    }

    /**
     * 裁剪
     */
    private fun jump2EditPage(path: String) {
        Navigation.findNavController(requireView()).navigateUp()
        mViewModel.getImagePath().value = path
//        val bundle = Bundle()
//        bundle.putString("imagePath", path)
//        Navigation.findNavController(requireView()).navigate(R.id.fragment_photo_editor, PhotoClipFragmentArgs.fromBundle(bundle).toBundle())
    }

    private fun loadUrlWithWG(
        imageView: ImageView,
        url: String
    ) {
        if (TextUtils.isEmpty(url)) return
        if (url.startsWith("http") || url.startsWith("https")) {
            ImageLoader.loadImage(imageView, url, fragment = this)
            return
        }
        ImageLoader.loadFitCenterImage(imageView, url, fragment = this)
    }
}